package com.easy.api.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @Author liuyuan
 * @Date 2020-04-24 15:42
 * @Version 1.0
 */

@Component
public class ApplicationContextUtils {

    public static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的回调方法，设置上下文环境
     *
     * @param applicationContext spring上下文对象
     * @throws BeansException 抛出spring异常
     */

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    private static void assertApplicationContext() {
        if (ApplicationContextUtils.applicationContext == null) {
            throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了applicaitonContext!");
        }
    }


}
